package whu.edu.controller;
import whu.edu.Entity.Supplier;
import whu.edu.Exception.ProductException;
import whu.edu.service.SupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author 86159
 */
@org.springframework.web.bind.annotation.RestController
@RequestMapping("Supplier")
public class SupplerController {
    @Autowired
    SupplyService supplyService;

    @GetMapping("")
    public Page<Supplier> getSupplier(String name,@RequestBody Integer pageN,@RequestBody Integer pageS){
        return supplyService.findSuppliers(name, PageRequest.of(pageN,pageS));
    }


    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable Integer id){
        Supplier result = supplyService.getSupplier(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }
    @PostMapping("")
    public Supplier addProduct(@RequestBody Supplier supplier) throws ProductException {
        return supplyService.addSupplier(supplier);
    }
    @PutMapping("/{id}")
    public void updateProduct(@PathVariable Integer id,@RequestBody Supplier supplier) throws ProductException {
        supplyService.updateSupplier(id,supplier);
    }
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Integer id){
        supplyService.deleteSupplier(id);
    }








}
